package com.zju.DoctorGo.controller;

import com.zju.DoctorGo.dto.BookingFilterDTO;
import com.zju.DoctorGo.entity.Booking;
import com.zju.DoctorGo.service.BookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/booking")
public class BookingController {

    @Autowired
    private BookingService bookingService;

    private String normalizeField(String field) {
        if (field == null || field.equals("0")) {
            return null;
        }
        return field;
    }

    @PostMapping
    public ResponseEntity<?> getBookings(@RequestBody BookingFilterDTO filter) {
        filter.setStatus(normalizeField(filter.getStatus()));
        filter.setDoctorId(normalizeField(filter.getDoctorId()));
        List<Booking> bookings = bookingService.getBookings(filter);
        return ResponseEntity.ok(bookings);
    }

    @PostMapping("/add")
    public ResponseEntity<?> addBooking(@RequestBody Booking booking) {
        bookingService.addBooking(booking);
        return ResponseEntity.ok().body("Booking added successfully");
    }

    @PostMapping("/delete")
    public ResponseEntity<?> deleteBooking(@RequestBody List<Long> bookingIds) {
        bookingService.deleteBooking(bookingIds);
        return ResponseEntity.ok().body("Booking deleted successfully");
    }
}
